<div class="msg" id="success">
  <table align="center">
    <tr>
      <td>
        <div class="checkout-success"></div>
      </td>
    </tr>
  </table>
</div>
<script>
    (function(){
      Zepto(function(){
        stretchScreen();
        $.each($(".bottom-info div"),function(index,item){
            item.style.removeProperty("z-index");
        });
      });
      $(window).on('resize',function(){
        stretchScreen();
      });
      function stretchScreen(){
        var topHeight = $('.top').height(),
        wrapper = $('.screen-wrapper'),
        minHeight;
        if(!wrapper.length) return;
        if(topHeight){
          minHeight = window.innerHeight-topHeight;
          wrapper = wrapper.css('min-height',minHeight).height();
          if(wrapper > minHeight)
            $('.screen-bottom').addClass('fixed');
        }
        $('.sm-martop').removeClass('sm-martop');
      }
      $('.J-toggleEye').on("click",function(){
        var input = $(this).parents().siblings('input')[0];
        input.type = input.type === "password"?"text":"password";
        $(this).toggleClass('gico-eye_closed');
      });
      $(".searchBar-sub").on("click",function(){
        if(!$(this).siblings("input[name='search_keywords']").val()){
          popup("商品不能为空",800,"sm-pop");
          $(this).preventDefault();
          return false;
        }
      });
    })();
    function changeSize(tar,wid){
        if(typeof tar.width === "undefined")
            tar = $(tar);
        wid = wid||tar.width();
        tar.find("img").width(wid).height(wid);
    }
    function popup(msg,time,klass,callback) {
        klass = klass || "";
        var tpl = $("<div class='popup "+klass+"'>" + msg + '</div>').appendTo(document.body);
        var timer = setTimeout(function(){
            tpl.remove();
            if(typeof callback !== "undefined") callback();
        }, time||3000);
    }
    function showError(msg){
      var ele = $(".form-feedback")[0];
      if(typeof ele === "undefined"){
        if(msg)
            alert(msg);
      }else{
        if(msg){
          $(ele).addClass('show').find("p").html(msg);
          setTimeout(function(){
            $(ele).removeClass('show');
          }, 2000);
        }
      }
    }
    (function(){
        $('.J-tab .trigger').on('tap',function(e){
            if($(this).attr('data-url') && $(this).attr('data-url') != 'true'){
                $.get($(this).attr('data-url'),function(re){
                    $($('.J-tab .panel')[$(this).index()]).append(re);
                }.bind(this));
                $(this).attr('data-url','true');
            }
            $(this).add($('.J-tab .panel')[$(this).index()]).addClass('act').siblings('.act').removeClass('act');
        });

        function changeVerify(element, hasEvent) {
            $.each(element,function(){
                var url;
                var img;
                var el = this;
                if(el.tagName === 'IMG') {
                    img = el;
                    url = el.getAttribute('src').split('?')[0];
                }
                else if(el.tagName === 'A') {
                    img = el.previousElementSibling;
                    url = el.getAttribute('href');
                }
                if(hasEvent) $(el).on('touchend click', function(e){e.preventDefault();}).on('tap longTap', changeCode.bind(el, img, url));
                else changeCode(img, url);
            });
        }
        function changeCode(img, url){
            url = url || img.src.split('?')[0];
            var random = +new Date();
            img.src = url + '?' + random;
            return false;
        }
        function toggleTips(obj){
          var children = obj.siblings('.c-tips').children('.g-icon');
          if(obj.val()!==""){
            children.removeClass('hide');
          }else{
            children.addClass('hide');
          }
        }
        changeVerify($('form .auto-change-verify-handle'), true);

        $(document).on('submit', 'form', function(){
            var sel = $('.region select'),
                region_false = false;
            $.each(sel,function(){
                if((this.style.visibility == 'visible') && (this.selectedIndex===0))
                    region_false = true;
            });
            if(region_false){
                alert('请选择完整地区！');
                return false;
            }

            var required = $(this).find('input[required]');

            if(required.length){
                var checkEmpty = false, ipt;
                $.each(required,function(){
                    ipt = $(this);
                    if(ipt.val()==='' && ipt.offset().height){
                        checkEmpty = ipt;
                        if(ipt.attr('data-caution'))
                            showError(ipt.attr('data-caution'));
                        return false;
                    }
                });
                if( checkEmpty )return false;
            }
            if($(this).attr('data-type') == 'ajax'){
                var self = $(this);
                $(this).find('[type=submit]').prop('disabled', true);
                $[$(this).attr('method')]($(this).attr('action'),$(this).serialize(),function(re){
                    try{
                        re = JSON.parse(re);
                    }catch(e) {}
                    var update = self.attr('data-update');
                    if(re.error){
                        if($('form .auto-change-verify-handle').length > 0){
                            $('form .auto-change-verify-handle').trigger('tap');
                        }
                        self.find('[type=submit]').prop('disabled', false);
                        return alert(re.error);
                    }else{
                        if($(update).size()) {
                            $(update).html(re);
                        }
                        else {
                            $('#success .checkout-success').html(re.success);
                            new Dialog('#success',{title:'提交成功！',type:'noclose'});
                            setTimeout(function(){location.href = re.redirect},1000);
                        }
                    }
                });
                return false;
            }else{
                return true;
            }
        });
        $(".form input").on("keyup",function(){
          var that = $(this);
          toggleTips(that);
        });
        $(".form input").on("focus",function(){
          var that = $(this);
          toggleTips(that);
        });
        
        $('.form .gico-close').on("click",function(){
          var parent = $(this).parent();
          console.dir(parent.children('.g-icon'));
          $(parent.siblings('input')[0]).val("");
          parent.children('.g-icon').addClass('hide');
        });
        function getCookie(name)
        {
            var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
         
            if(arr=document.cookie.match(reg))
                return unescape(arr[2]);
            else
                return '';
        } 
        var UNAME = $.fn.cookie('UNAME')?$.fn.cookie('UNAME'):'';
        cartNum = getCookie("S[CART_NUMBER]");
        if(UNAME){
            $("#footerloginuname").text(decodeURIComponent(UNAME));
            $("#footerlogout").show();
            if($("#footerlogin"))
            $("#footerlogin").hide();
        }else{
            $("#footerlogin").show();
            if($("#footerlogout"))
            $("#footerlogout").hide();
        }
        if(cartNum)$('.cart-num').html(cartNum).show();
        else $('.cart-num').hide();
    })();
</script>

<{if $from_weixin}>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>

var imgUrl = "<{$weixin.imgUrl}>";
var lineLink = "<{$weixin.linelink}>";
var shareTitle = "<{$weixin.shareTitle}>";
var descContent = "<{$weixin.descContent}>";
wx.config({
    debug: false,
    appId: '<{$signPackage.appId}>',
    timestamp: '<{$signPackage.timestamp}>',
    nonceStr: '<{$signPackage.nonceStr}>',
    signature: '<{$signPackage.signature}>',
    jsApiList: [
      // 所有要调用的 API 都要加到这个列表中
      'onMenuShareTimeline',
      'onMenuShareAppMessage',
      'onMenuShareQQ',
      'onMenuShareWeibo',
      'checkJsApi'
    ]
  });
wx.ready(function(){
    wx.checkJsApi({
        jsApiList: ['onMenuShareAppMessage','onMenuShareTimeline','onMenuShareQQ'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
            // 以键值对的形式返回，可用的api值true，不可用为false
            // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
    });
    //发送给朋友
    wx.onMenuShareAppMessage({
    title: shareTitle, // 分享标题
    desc: descContent, // 分享描述
    link: lineLink, // 分享链接
    imgUrl: imgUrl, // 分享图标
    type: 'link', // 分享类型,music、video或link，不填默认为link
    dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
    trigger: function (res) {
        //用户点击发送给朋友
    },
    success: function () {
        // 用户确认分享后执行的回调函数
    },
    cancel: function () {
        // 用户取消分享后执行的回调函数
    }
    });
    //分享到朋友圈
    wx.onMenuShareTimeline({
        title: shareTitle, // 分享标题
        link: lineLink, // 分享链接
        imgUrl: imgUrl, // 分享图标
        success: function () { 
            // 用户确认分享后执行的回调函数
        },
        cancel: function () { 
            // 用户取消分享后执行的回调函数
        }
    });

    //分享到QQ
    wx.onMenuShareQQ({
      title: shareTitle, // 分享标题
      desc: descContent, // 分享描述
      link: lineLink, // 分享链接
      imgUrl: imgUrl, // 分享图标
      success: function () { 
         // 用户确认分享后执行的回调函数
      },
      cancel: function () { 
         // 用户取消分享后执行的回调函数
      }
  });
    //分享到腾讯微博
    wx.onMenuShareWeibo({
      title: shareTitle, // 分享标题
      desc: descContent, // 分享描述
      link: lineLink, // 分享链接
      imgUrl: imgUrl, // 分享图标
      success: function () { 
         // 用户确认分享后执行的回调函数
      },
      cancel: function () { 
          // 用户取消分享后执行的回调函数
      }
  });
});
</script>
<{/if}>
